package remoteRpc

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/zcamp-service-user/app/rpc/proto"
	"google.golang.org/grpc"
	"sync"
)

var (
	userRpc          *RemoteUserRpc
	onceUserRpcCache sync.Once
)

func GetUserRpc() *RemoteUserRpc {
	if userRpc == nil {
		baseLog.Errorf("userRpc instance is %v", userRpc)
	}
	return userRpc
}

type RemoteUserRpc struct {
	UserRpcServiceClient proto.UserRpcServiceClient
}

func InitUserRpc(rpcAddr string) (result *RemoteUserRpc, err error) {
	onceUserRpcCache.Do(func() {
		conn, tmperr := grpc.Dial(rpcAddr, grpc.WithInsecure())
		if tmperr != nil {
			err = tmperr
			return
		}
		userRpc = &RemoteUserRpc{
			UserRpcServiceClient: proto.NewUserRpcServiceClient(conn),
		}
		result = userRpc
	})
	return
}
